package com.inspur.cloud.dao.dataspace;

import com.inspur.cloud.entity.dataspace.UserEntity;
import com.inspur.cloud.entity.dataspace.UserResourceRelationEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserResourceRelationDao extends JpaRepository<UserResourceRelationEntity, Integer>, JpaSpecificationExecutor<UserResourceRelationEntity> {

    List<Integer> findUserIdByUserIdIn(List<Integer> userIdList);

    UserResourceRelationEntity findByResourceIdAndUserId(@Param("resourceId") Integer resourceId, @Param("userId") Integer userId);

    UserResourceRelationEntity findByResourceIdAndUserIdAndResourceType(@Param("resourceId") Integer resourceId, @Param("userId") Integer userId, @Param("resourceType") String resourceType);

    @Query("select r from UserResourceRelationEntity r WHERE r.resourceId = :resourceId AND r.userId=:userId AND r.resourceType=:resourceType")
    List<UserResourceRelationEntity> findAllResourceIdAndUserIdAndResourceType(@Param("resourceId") Integer resourceId, @Param("userId") Integer userId, @Param("resourceType") String resourceType);

    UserResourceRelationEntity findFirstByResourceIdAndUserIdAndResourceType(@Param("resourceId") Integer resourceId, @Param("userId") Integer userId, @Param("resourceType") String resourceType);

    UserResourceRelationEntity findFirstByUserIdAndResourceType(@Param("userId") Integer userId, @Param("resourceType") String resourceType);

    List<UserResourceRelationEntity> findByResourceIdAndResourceType(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType);

    List<UserResourceRelationEntity> findByUserIdAndResourceType(Integer userId, String resourceType);

    @Modifying
    @Query("delete from UserResourceRelationEntity r where r.resourceId = :resourceId  AND r.resourceType=:resourceType ")
    Integer delByResourceId(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType);

    @Modifying
    @Query("delete from UserResourceRelationEntity r where r.userId = :userId")
    Integer delByUserId(@Param("userId") Integer userId);

    @Modifying
    @Query("delete from UserResourceRelationEntity r where r.resourceId = :resourceId AND r.userId!=:userId AND r.resourceType=:resourceType ")
    Integer delByResourceIdExcludeOwner(@Param("resourceId") Integer resourceId, @Param("userId") Integer userId, @Param("resourceType") String resourceType);

    @Modifying
    @Query("delete from UserResourceRelationEntity r where r.resourceId = :resourceId AND r.userId in :userList AND r.resourceType=:resourceType ")
    Integer delByResourceIdWhereIdIn(@Param("resourceId") Integer resourceId, @Param("userList") List<Integer> userList, @Param("resourceType") String resourceType);

    @Query("select u.name from UserResourceRelationEntity ur INNER JOIN UserEntity u ON ur.userId=u.id where ur.resourceType=:resourceType AND ur.resourceId=:resourceId")
    List<String> queryUserNameByResourceId(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType);

    @Query("select u from UserResourceRelationEntity ur INNER JOIN UserEntity u ON ur.userId=u.id where ur.resourceType=:resourceType AND ur.resourceId=:resourceId")
    List<UserEntity> queryUserByResourceId(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType);

    @Query("select u from UserResourceRelationEntity ur INNER JOIN UserEntity u ON ur.userId=u.id where ur.resourceType=:resourceType AND ur.resourceId=:resourceId AND u.parentId=:parentId")
    List<UserEntity> queryMySonByResourceId(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType, @Param("parentId") Integer parentId);

    @Query("select u from UserResourceRelationEntity ur INNER JOIN UserEntity u ON ur.userId=u.id where ur.resourceType=:resourceType AND ur.resourceId=:resourceId AND u.id !=:owner")
    List<UserEntity> queryUserByResourceIdExceptOwner(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType, @Param("owner") Integer owner);


    List<UserResourceRelationEntity> findByUserIdInAndResourceIdInAndResourceType(List<Integer> userIds, List<Integer> resourceIds, String resourceType);

    @Modifying
    @Query("delete from UserResourceRelationEntity r where r.resourceId in :resourceIdList AND  r.resourceType=:resourceType ")
    Integer delByResourceIdList(@Param("resourceIdList") List<Integer> resourceIdList, @Param("resourceType") String resourceType);

    @Query("select u.id from UserResourceRelationEntity ur INNER JOIN UserEntity u ON ur.userId=u.id where ur.resourceType=:resourceType AND ur.resourceId=:resourceId AND u.role=20 ")
    List<Integer> findTennatsByResourceId(@Param("resourceId") Integer resourceId, @Param("resourceType") String resourceType);

}
